import api from '../index'
import type { ResponseData, ResponseRowsRes, RequestFromType, pageParam } from '../type'
import type { GoodsItem, StoreInfo, GoodsDetail, CartGoodsItem, PreviewOneGoodsOrderParams, PreviewOrderData, PurchaseGoodsPayData, PurchaseGoodsParams, AccountItem, GoodsEvaluateItem } from '.'

export interface NearbyStoreParams {
	industry ?: number
	keyword ?: string
	orderByType ?: number
	orderByCollation ?: number
	beginTime ?: string
	endingTime ?: string
}

export interface NearbyStoreInfo extends StoreInfo {
	evaluateCount:number //评价总数
	evaluateCountPhoto:number //带图评价总数
	beginTime:string//营业开始时间
	endingTime:string//营业结束时间
	shopkeeperTelephone:string//商家电话
	realScenePhoto:string //门头照
	distance:number//距离
	location:string //经纬度
	licenseAddrText:string //地址
}
// 附近店铺列表
export const getNearbyStoreList = (data : pageParam & NearbyStoreParams) =>
	api.get<ResponseRowsRes<NearbyStoreInfo>>('merchant/customer/store/nearby/list', {
		data,
		custom: {
			auth: false,
			isLocation: true
		}
	})
// 附近店铺详情
export const getNearbyStoreDetail = (id : string) =>
	api.get<ResponseData<NearbyStoreInfo>>(`merchant/customer/store/nearby/${id}`, {
		custom: {
			auth: false,
			isLocation: true
		}
	})
export interface StoreGoodsItem extends GoodsItem {
	productType : 1 | 2
}
// 店铺商品列表
export const getNearbyStoreGoodsList = (data : {
	storeId : string,
	groupingId ?: string | number
} & pageParam) =>
	api.get<ResponseRowsRes<StoreGoodsItem>>('product/customer/nearby/list', {
		data,
		custom: {
			auth: false
		}
	}
	)
export interface StoreGoodsDetail extends GoodsDetail {
	description : {
		detailRichTextMobile : string
		detailRichTextPc : string
		imgUrls : string
		verifyDetail ?: string
		verifyRule ?: string
		videoUrl : string
	}
	productType:number

}
// 店铺商品详情
export const getStoreGoodsInfo = (data : { id : string }) =>
	api.get<ResponseData<StoreGoodsDetail>>('product/customer/nearby/detail', {
		data,
		custom: {
			auth: false
		}
	})


export interface StoreEvaluateItem {
	id : number;
	customerHeaderImg : string; //头像
	customerNickName : string; //名称
	averageScore : number; //评分
	serviceScore : number; //服务评分
	descScore : number; //描述评分
	createTime : string; //评价时间
	skuSpec : string; //商品规格信息
	content ?: string; //评价内容
	photos ?: string[]; //评价图片
	hasStoreReply : 0 | 1; //商家是否回复
	hasAnonym : 0 | 1;//是否匿名
	contentStoreReply : string; //商家回复内容
}
// 店铺评价列表
export const getStoreEvaluationList = (data : {
	storeId : string
} & pageParam) =>
	api.get<ResponseData<StoreEvaluateItem[],true>>('product/customer/nearby/evaluate/list', {
		data,
		custom: {
			auth: false
		}
	})
export interface StoreCartGoodsItem extends CartGoodsItem {

}
// 店铺购物车列表
export const getStoreCart = (id : string) =>
	api.get<ResponseData<StoreCartGoodsItem[]>>(`customer/api/basket/offline/list/${id}`)
// 添加到店铺购物车
export const postAddStoreCart = (data : {
	skuId : string,
	basketCount : number
}) =>
	api.post('customer/api/basket/offline', {
		data
	})
// 店铺购物车编辑
export const postEditStoreCart = (data : {
	skuId : string,
	basketCount : number
}) =>
	api.put('customer/api/basket/offline', {
		data
	})

// 删除店铺购物车的商品
export const postDelStoreCartItem = (id : number | string) =>
	api.delete(`customer/api/basket/offline/${id}`)

// 清空店铺购物车
export const postDelStoreCart = (id : string) =>
	api.delete(`customer/api/basket/offline/clear/${id}`,)

// 线下门店商品下单预览
export const postPreviewNearbyStoreGoodsOrder = (data : {
	storeId:string
	receivedId:number|string
}) =>
	api.post<ResponseData<PreviewOrderData>&{today:string,tomorrow:string}>('customer/api/offline/order/create/basket/pre', {
		data
	})
// 商品购买
export const postPurchaseNearbyStoreGoods = (data : PurchaseGoodsParams) =>
	api.post<PurchaseGoodsPayData>('customer/api/offline/order/create/basket', {
		data
	})
export interface deductionAccountItem extends AccountItem {
	deductMoney ?: number
	accountMoney : number
	check : boolean
	icon : string
}
// 店铺买单优惠查询
export const getStorePaymentPre = (data : { storeId : string, money ?: string | number, deductionStrategy ?: string }) =>
	api.get<ResponseData<{
		paymentDis : number
		deductionMode : string//Y 抵扣账户需足额抵扣，N 抵扣账户可不足额抵扣
		deductionType : string//Y 可多选账户抵扣，N 只能单选账户抵扣
		deductions : deductionAccountItem[]
		payMoney : number//总计
		payDeductMoney : number//买单优惠金额 
		paymentDisMoney : number//总抵扣
	}>>('customer/api/store/pay/paymentPre', {
		data
	})
// 店铺买单
export const postStorePay = (storeId : string, data : {
	fromType ?: RequestFromType
	money : string | number
	payId : number
	deductionStrategy ?: string
}) =>
	api.post<PurchaseGoodsPayData>(`customer/api/store/pay/money/${storeId}`, {
		data
	})

// 店铺买单支付结果查询
export const getNearbyStorePayResult = (id : string) =>
	api.get<PurchaseGoodsPayData>(`order/api/main/payCode/${id}`)

export interface NearbyStoreGoodsEvaluateItem extends GoodsEvaluateItem {

}
// 店铺买单评价
export const postPayEvaluate = (data : {
	orderId : string
	score : number
	content ?: string
	photos ?: string
}) =>
	api.post<ResponseData<NearbyStoreGoodsEvaluateItem, true>>('product/customer/nearby/evaluate/add', {
		data,
		custom: {
			isLoading: true
		}
	})
export interface NearbyStorePayDetailItem {
	id:number
	payName:string
	storeLogo:string//店铺logo
	ruleData:string//店铺名称
	payTime:string
	moneyPay:number

}
export interface NearbyStorePayDetailResponseData extends ResponseData<PagingData<NearbyStorePayDetailItem>> {
	moneyPay:number
	count:number
}
// 店铺买单支付明细
export const getNearbyStorePayDetail = (data : pageParam & {
	startTime?:string
	endTime?:string
}) =>
	api.get<NearbyStorePayDetailResponseData>('order/api/pay/detail/storePayment',{
		data
	})